package T04_数据库连接方式举例.F03_连接方式三;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;

/**
 * 连接方式三
 * 说明:使用DriverManager实现数据库的连接。体会获取连接必要的4个基本要素。
 */
public class TestConnection03 {
    public static void main(String[] args) {
        try {

            //1.数据库连接的4个基本要素：
            String url = "jdbc:mysql://localhost:3306/test";
            String user = "root";
            String password = "abc123";
            String driverName = "com.mysql.jdbc.Driver";

            //2.实例化Driver
            Class clazz = Class.forName(driverName);
            Driver driver = (Driver) clazz.newInstance();

            //3.注册驱动
            DriverManager.registerDriver(driver);

            //4.获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
